<?php
/**
 * Created by PhpStorm.
 * User: cyh
 * Date: 2017/4/29
 * Time: 20:13
 */

namespace app\db\controller;


use app\db\model\AdminInf;
use app\db\model\PlanInf;
use app\db\model\RemenberInf;
use app\db\model\UserInf;
use app\db\model\WordgroupInf;
use app\db\model\WordInf;
use think\Controller;
use think\Db;
use think\Exception;
use think\Request;

class File extends  Controller
{

    private   $postname='file';
    /***
     * 保存上传图片
     * 保持到pic目录下返回相对路径
     * @return string
     */
    function  UpFile()
    {
        ///获取头参数
        $picfile=Request::instance()->header()['picfile'];//文件字段名
        $typename=Request::instance()->header()['typename'];//表名
        $key=Request::instance()->header()['key'];//主键值
        $type=Request::instance()->header()['type'];//文件类型
        //存储图片
        switch ($type)
        {
            case 'mp3':$info=$this->GetFile(ROOT_PATH . 'public/'.'mp3');break;
            case 'jpg':$info=$this->GetFile(ROOT_PATH . 'public/'.'pic');break;
            case 'mp4':$info=$this->GetFile(ROOT_PATH . 'public/'.'mp4');break;
            default :return "unknow Filetype";break;
        }
        return $info;
    }
    function GetFile($savepath)
    {
        try {
            $file = request()->file($this->postname);//file
            $info = $file->move($savepath);//
            //返回信息
            if ($info) {
                // 成功上传后 获取上传信息
                // 输出 路径
                return $info->getSaveName();
            } else {
                // 上传失败
                return "error";
            }
        }
        catch (Exception $ec)
        {
            return $ec->getMessage();
        }
    }


}